jQuery.validator.addMethod("ip", function(value, element) {
	return this.optional(element) || (/^(\d+)\.(\d+)\.(\d+)\.(\d+)$/.test(value) && (RegExp.$1 <256 && RegExp.$2<256 && RegExp.$3<256 && RegExp.$4<256));
}, "请输入合法的IP信息"); 

jQuery.validator.addMethod("port", function(value, element) {
	return this.optional(element) || (/^(\d+)$/.test(value) && (RegExp.$1 > 0 && RegExp.$1 < 65536));
}, "请输入合法的端口信息"); 

String.prototype.replaceAll  = function(s1,s2){     
    return this.replace(new RegExp(s1,"gm"),s2);     
}

$('#btn_submit').click(function () {
    $("#install_form").submit();
});

$("#install_form").validate({
    rules: {
    	ip: {
            required: true,
            ip: true
        },
        port: {
            required: true,
            port:true
        },
        user: {
            required: true
        },
        passwd: {
            required: true
        }
    },
    messages: {
    	ip: "请输入合法的外网ip",
    	port: "请输入合法的端口号",
    	user: "请输入用户名",
    	passwd:"请输入密码"
    },
    errorElement: "span",
    submitHandler: function(form) {
    	// 检查是否有主机正在装新服
    	var url = CONTEXT_PATH + '/host/init/checkDuplicate';
    	var ip = $('#ip').val();
    	
    	$.wajax({
            type: "POST",
            url: url,
            data: {ip: ip},
            success: function(result) {
            	// 如果没有就继续
                if (result.data != null && result.data == '0') {
                	// host_init对应Common.HOST_INIT_DIV_ID_PREFIX
                	parent.parent.parent.makeGlobalLayer('正在初始化主机：' + ip, 'host_init_' + ip.replaceAll('\\.', '_'));
                    
                    url = CONTEXT_PATH + '/host/init/init';
                    
                    $.wajax({
                        type: "POST",
                        url: url,
                        data: $(form).serialize(),
                        success: function(result) {
                            //layer.alert(result.msg);
                            $(form)[0].reset();
                        }
                    });
                } else {
                	layer.msg(result.msg, {
                		offset: 't',
                		anim: 6
            		});
                }
            }
        });
    }
});//End